#ifndef DEVICEINFO_H
#define DEVICEINFO_H

#include <QDialog>
#include <QFutureWatcher>

class FtpManager;
class BroadThread;
class QHostAddress;
class TcpDepthWorker;
class FTPClient;
class RgbCapture;
class TcpProtocolWorker;
class TcpIrWorker;
class QTableWidgetItem;
class DepthCloudDataProcessor;

enum UploadFileInfo
{
    Upgrade,
    ConfigurationRegister
};
namespace Ui {
class DeviceInfo;
}

class DeviceInfo : public QDialog
{
    Q_OBJECT

public:
    explicit DeviceInfo(QWidget *parent = nullptr);
    ~DeviceInfo();


    // 获取FTP配置
    QString getFtpUrl() const;
    QString getFtpUsername() const;
    QString getFtpPassword() const;
    QString getFtpRemoteDir() const;



public slots:
    void onTcpDataReceived(const QByteArray& message,const QHostAddress& adress);
 
    void startPointCloudSlot();

    void onRectReStartCommand();

    //void onProtocolConnect();
    //void onProtocolDisConnect();
    //void onDepthConnect();
    //void onDepthDisConnect();
    //void onIrConnect();
    //void onIrDisConnect();

private slots:
    void on_SearchDeviceBtn_clicked();

    void on_upgradeBtn_clicked();

    void on_SetIPBtn_clicked();

    void on_restartBtn_clicked();

    void on_syncTimeBtn_clicked();

    void handleBroadcastMessage(const QByteArray& message, const QHostAddress& adress);

    void handleHeartbeatMessage(const QByteArray& message);

    void handleTimeSyncMessage(const QByteArray& message);

    void handleDeviceInfoMessage(const QByteArray& message);

    void handleDiagnosticsMessage(const QByteArray& message);

    void handleUpgradeStatusMessage(const QByteArray& message);

    void sendHeartBeat();

    void on_ipConnectBtn_clicked();

    void handleRebootCommand(const QByteArray& message);

    void handleSetIpMessage(const QByteArray& message);

    void handleConfigurationRegisterMessage(const QByteArray& message);

    void handleGetDepthInfoMessage(const QByteArray& message);

    void on_gradeStatusBtn_clicked();

    void onTcpConnected();
    void onTcpDisconnected();
    void onTabChanged(int index);
    void on_ConfigurationRegisterBtn_clicked();

    void on_getDire_clicked();
    void onDirectoryListingReady(const QStringList& files);
    void onDirectoryListingError(const QString& errorMessage);

    void on_batchDownloadButton_clicked();
    void onDownloadFinshed();
    void onDownloadProgress(const QString& fileName,int progress);
    void on_uploadButton_clicked();
    void onUploadProgress(const QString& fileName, int progress);
    void onUploadFinished(const QString& fileName, bool success);
    void onUploadError(const QString& fileName, const QString& errorMessage);

    void sendGetDepthCommand();
    
    void buildConnectDepth();

    void stopDepthTimer();

    void downLoadAllNodeYaml();
signals:
    void sendTcpData(const QByteArray& message);
    void startConnectTcp(const QString&,quint16);
    void startPointCloudSignal(bool flag);
    void sendRtspIpAddress(const QString& ip);

    void sendInitGeneral();

    void sendDepthAndIRDisConnectSignal();
private:
    Ui::DeviceInfo *ui;

    QTimer* m_timer;
    //FtpManager* m_ftpManager;

    BroadThread* broadThread;

    QString m_ipStr;
    quint16 m_port;

    QThread* depthThread;
    TcpDepthWorker* tcpDepthWorker;

    void upLoadUpFileSwitch(UploadFileInfo);
    UploadFileInfo uploadFileType;

    FTPClient* m_ftpClient;
    // 新增：目录列表获取的监视对象
    QFutureWatcher<QStringList> m_directoryWatcher;
    void manageBatchDownload(const QList<QFuture<bool>> &futures);
    void manageUploadTask(QFuture<bool> future, const QString& fileName);
    QFutureWatcher<bool> m_downloadWatcher;

    QThread* protocolThread;
    TcpProtocolWorker* tcpProtocolWorker;

    QThread* irThread;
    TcpIrWorker* tcpIrWorker;

    DepthCloudDataProcessor* depthCloudDataProcessor;


    QTimer* m_timerConnectDepth;
    //QTableWidgetItem* protocolItem;
    //QTableWidgetItem* depthItem;
    //QTableWidgetItem* irItem;
    int downLoadType;

    QFuture<bool> checkFileAsync(const QString& filePath);
};

#endif // DEVICEINFO_H
